/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import dbs.User;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 */
@Stateless
public class UserFacade extends AbstractFacade<User> {
    @PersistenceContext(unitName = "OurConnectionServer-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }
    
    public User findUser(long facebookId)
    {
        TypedQuery<User> query =  em.createNamedQuery("User.findByFacebookId", User.class);
        //TypedQuery<User> query = em.createQuery("SELECT u FROM User u WHERE u.facebookId = :facebookId", User.class);
        //query.setParameter("facebookId", facebookId);
        query.setParameter("facebookId", facebookId);
        
        for(User user : query.getResultList())
        {
            // exist
            return user;
        }
        // not exist
        return null;
    }
}
